﻿//################################################################
//################################################################
//                   METHODS DEFINED
//###############################################################
//###############################################################
function contains(value, array) 
{
    for (var i = 0; i < array.length; i++) {
        if (value == array[i]) 
        {
           
            return 1;                       //create a function which will compare an item to what is stored in a particular array

        }
        else {
            if (i + 1 == array.length)          //if we have reached the end of the array then the value does not match anything in the array
            {
                return -1;
            }
        }
    }

}
function findPlaces(locatePlaces) {

    if (markersArray.length > 0) {
        deleteOverlays();                    //if any markers are present from a previous search then delete them

    }
    var request =
    {
        location: map.getCenter(),       
        radius: 500,                             //  object literal which contains the information needed to search for resteraunts,cafes etc
        types: [locatePlaces]
    };



    var service = new google.maps.places.PlacesService(map);

    service.nearbySearch(request, callback);
    
}

function callback(results, status, pagination) 
{
    if (status != google.maps.places.PlacesServiceStatus.OK) {  //callback used to generate markers from the nearby search result
        return;
    } else {
        createMarkers(results);


    }
}

function createMarkers(places) {
    var bounds = new google.maps.LatLngBounds();

    for (var i = 0, place; place = places[i]; i++) {
        var image = new google.maps.MarkerImage(
              place.icon, new google.maps.Size(71, 71),                       //create images and markers from the results retrieved from finding the resteraunts
              new google.maps.Point(0, 0), new google.maps.Point(17, 34),     //Gives a visual representation of your results
              new google.maps.Size(25, 25));

        var marker = new google.maps.Marker({
            map: map,
            icon: image,
            title: place.name,
            position: place.geometry.location
        });


        markersArray.push(marker);
        bounds.extend(place.geometry.location);
    }
    map.fitBounds(bounds);
}

function deleteOverlays() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
        markersArray.length = 0;
    }
}



function accessDialog(text,heading) 
{
    $('#dialog').dialog('option', 'title', heading);
    document.getElementById("dialog").innerHTML = text;      //Used o display a message to the user when saving and loading locations
    $("#dialog").dialog("open");

}
function modifyMapType(mapType) 
{
    map.setMapTypeId(mapType);  //change to relevant mapType(Satellite,Terrain etc)

}


function processSVData(data, status)                  //callback function used for getPnaoramaByLocation to update the co-ordinates when going forward or backwards
{
    if (status == google.maps.StreetViewStatus.OK) 
    {
        map.setCenter(updateCoordinates);
        panorama.setPosition(updateCoordinates);
    }
    else {
        accessDialog("The Street View is not available for the location you have specified!!");

    }

}

function ZoomInOutMap(zoom) 
{
    map.setZoom(zoom);             //Zoom in or out in the map view

}

function ModifyHeadingOrPitch(heading, pitch, zoom) 
{

    panorama.setPov
    ({
        heading: heading,
        pitch: pitch,               //Update heading pitch or zoom on the street view
        zoom: zoom
    });

}

function ModifyLocation(updateCoordinates, heading, pitch) //Used when re-directing to a different city
{
  
    map.setCenter(updateCoordinates);
    panorama.setPosition(updateCoordinates);
    panorama.setPov
    ({
        heading: heading,
        pitch: pitch
    });

}
//functions for going forward or backwards on street view
function goingForward()
 {

     if (lookPosition >= 0 && lookPosition < 90) 
     {
         updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[0].AlterLat, lngpoint + coOrdinateCalculations[0].AlterLng);

     }
     else if (lookPosition >= 90 && lookPosition < 180) {
         updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[1].AlterLat, lngpoint + coOrdinateCalculations[1].AlterLng);
     }
     else if (lookPosition >= 180 && lookPosition < 270) {
         updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[0].AlterLat, lngpoint - coOrdinateCalculations[0].AlterLng);
     }
     else {
         updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[1].AlterLat, lngpoint - coOrdinateCalculations[1].AlterLng);
     }

 }

 function goingBack() 
 {

     if (lookPosition >= 0 && lookPosition < 90) {
         updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[0].AlterLat, lngpoint - coOrdinateCalculations[0].AlterLng);

     }
     else if (lookPosition >= 90 && lookPosition < 180) {
         updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[1].AlterLat, lngpoint - coOrdinateCalculations[1].AlterLng);
     }
     else if (lookPosition >= 180 && lookPosition < 270) {
         updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[0].AlterLat, lngpoint + coOrdinateCalculations[0].AlterLng);
     }
     else {
         updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[1].AlterLat, lngpoint + coOrdinateCalculations[1].AlterLng);
     }

 }
 function turnLeft() 
 {

     if (lookPosition >= 0 && lookPosition < 90) 
     {
         updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[1].AlterLat, lngpoint - coOrdinateCalculations[1].AlterLng);

     }
     else if (lookPosition >= 90 && lookPosition < 180) {
         updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[0].AlterLat, lngpoint + coOrdinateCalculations[0].AlterLng);
     }
     else if (lookPosition >= 180 && lookPosition < 270) {
         updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[1].AlterLat, lngpoint + coOrdinateCalculations[1].AlterLng);
     }
     else 
     {
         updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[0].AlterLat, lngpoint - coOrdinateCalculations[0].AlterLng);
     }

 }
 function turnRight() 
 {

     if (lookPosition >= 0 && lookPosition < 90) 
     {
         updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[1].AlterLat, lngpoint + coOrdinateCalculations[1].AlterLng);

     }
     else if (lookPosition >= 90 && lookPosition < 180) 
     {
         updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[0].AlterLat, lngpoint - coOrdinateCalculations[0].AlterLng);
     }
     else if (lookPosition >= 180 && lookPosition < 270) 
     {
         updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[1].AlterLat, lngpoint - coOrdinateCalculations[1].AlterLng);
     }
     else 
     {
         updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[0].AlterLat, lngpoint + coOrdinateCalculations[0].AlterLng);
     }


 }
























